package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysDevice;
import com.ruoyi.system.domain.SysOrder;
import com.ruoyi.system.mapper.SysDeviceMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysOrderMapper;
import com.ruoyi.system.service.ISysDeviceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色信息表(SysDevice)表服务实现类
 *
 * @author makejava
 * @since 2022-03-16 10:44:32
 */
@Service
public class SysDeviceServiceImpl implements ISysDeviceService {

    @Resource
    private SysDeviceMapper sysDeviceMapper;

    @Resource
    private SysOrderMapper sysOrderMapper;

    @Resource
    private SysDictDataMapper dictDataMapper;


    @Override
    public List<SysDevice> selectDeviceList(SysDevice device) {
        return sysDeviceMapper.queryAll(device);
    }

    @Override
    public List<SysDevice> selectDeviceAll() {
        return sysDeviceMapper.queryAll(new SysDevice());
    }

    @Override
    public SysDevice selectDeviceById(Long deviceId) {
        return sysDeviceMapper.queryById(deviceId);
    }

    @Override
    public boolean deleteDeviceById(Long deviceId) {
        sysDeviceMapper.deleteById(deviceId);
        return true;
    }

    @Override
    public int deleteDeviceByIds(String ids) {
        Long[] deviceIds = Convert.toLongArray(ids);
        for (Long deviceId : deviceIds){
            sysDeviceMapper.deleteById(deviceId);
        }
        return 1;
    }

    @Override
    public int insertDevice(SysDevice device) {
        device.setDelFlag("0");
        device.setCreateTime(new Date());
        return sysDeviceMapper.insert(device);
    }

    @Override
    public int updateDevice(SysDevice device) {
        device.setUpdateTime(new Date());
        return sysDeviceMapper.update(device);
    }

    @Override
    public String checkDeviceNameUnique(SysDevice device) {
        Long deviceId = StringUtils.isNull(device.getDeviceId()) ? -1L : device.getDeviceId();
        SysDevice info = sysDeviceMapper.checkDeviceNameUnique(device.getDeviceName());
        if (StringUtils.isNotNull(info) && info.getDeviceId().longValue() != deviceId.longValue())
        {
            return UserConstants.ROLE_NAME_NOT_UNIQUE;
        }
        return UserConstants.ROLE_NAME_UNIQUE;
    }

    @Override
    public String checkDeviceCodeUnique(SysDevice device) {
        Long deviceId = StringUtils.isNull(device.getDeviceId()) ? -1L : device.getDeviceId();
        SysDevice info = sysDeviceMapper.checkDeviceCodeUnique(device.getDeviceCode());
        if (StringUtils.isNotNull(info) && info.getDeviceId().longValue() != deviceId.longValue())
        {
            return UserConstants.ROLE_NAME_NOT_UNIQUE;
        }
        return UserConstants.ROLE_NAME_UNIQUE;
    }

    @Override
    public int changeStatus(SysDevice device) {
        return sysDeviceMapper.update(device);
    }

    @Override
    public int pay(SysDevice device, SysUser sysUser, String orderCode, String phone, String address, Double amount, String remark) {
        device = sysDeviceMapper.queryById(device.getDeviceId());
        SysOrder sysOrder = sysOrderMapper.queryByOrderCode(orderCode);
        if(StringUtils.isNull(sysOrder)) {
            sysOrder = new SysOrder();
        }
        sysOrder.setOrderCode(orderCode);
        sysOrder.setUserId(sysUser.getUserId());
        sysOrder.setDeviceId(device.getDeviceId());
        sysOrder.setDeviceCode(device.getDeviceCode());
        sysOrder.setDeviceName(device.getDeviceName());
        sysOrder.setDeviceType(device.getDeviceType());
        sysOrder.setPhone(phone);
        sysOrder.setAddress(address);
        sysOrder.setAmount(amount);
        sysOrder.setRemark(remark);
        sysOrder.setStatus("0");
        sysOrder.setPayStatus("0");
        sysOrder.setDelFlag("0");
        sysOrder.setCreateBy(sysUser.getLoginName());
        sysOrder.setCreateTime(new Date());
        if(StringUtils.isNull(sysOrder.getOrderId())) {
            sysOrderMapper.insert(sysOrder);
        }else{
            sysOrderMapper.update(sysOrder);
        }
        device.setStatus("0");
        return sysDeviceMapper.update(device);
    }

    @Override
    public Map<String,Object> deviceTypeTools(Long deviceId) {
        Map<String,Object> map = new HashMap<>();
        SysDevice info = sysDeviceMapper.queryById(deviceId);
        String deviceType = dictDataMapper.selectDictLabel("device_type",info.getDeviceType().toString());
        map.put("dictLabel",deviceType);
        return map;
    }
}
